package com.bdk.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.bdk.conmon.ResultCode;
import com.bdk.conmon.ResultEntity;
import com.bdk.conmon.SystemCons;
import com.bdk.dao.RoleMapper;
import com.bdk.mapper.Role;
import com.bdk.mapper.User;
import com.bdk.model.PageJson;
import com.bdk.model.RoleVO;
import com.bdk.service.IRoleService;
import com.bdk.utill.JSONUtils;
import com.bdk.utill.StringHelper;

/**
 * 用户相关实现 ClassName: UserServiceImpl
 * 
 * @Description:
 * @author ChenQuan
 * @date 2017年5月4日下午1:57:23
 */
@Service("roleService")
public class RoleServiceImpl implements IRoleService {
	final Logger logger = LoggerFactory.getLogger(getClass());
	@Resource
	private RoleMapper roleDao;
	ResultEntity<RoleVO> result = new ResultEntity<RoleVO>();
	
	
	
	@Override
	public List<Role> queryRoleList() {
		return roleDao.queryRoleList();
	}



	@Override
	public void updateRoleByVo() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public ResultEntity<RoleVO> addRoleVo(HttpServletRequest request, RoleVO role) {
		  ResultEntity<RoleVO> result = new ResultEntity<RoleVO>();
		  Role roleEntity=new Role();
		  BeanUtils.copyProperties(role, roleEntity);
			 if(SystemCons.checkLoging(request))//登录的逻辑
		    {
				try {
					roleEntity.setRoleId(UUID.randomUUID().toString());//id为随机数
					roleDao.insert(roleEntity);
					result.setErrorMsg("添加成功");
					logger.info("角色添加--成功！");
				} catch (Exception e) {
					result.setCode(ResultCode.ERRO.getValue());
					result.setErrorMsg(ResultCode.ERRO.getName());
					logger.info("角色添加异常--信息！");
				}
			    return result;
			}else{//未登录的逻辑
				result.setCode(ResultCode.NOTLOGIN.getValue());
				result.setErrorMsg(ResultCode.NOTLOGIN.getName());
				logger.info("角色未登录");
				return result;
			}
	}

	@Override
	public ResultEntity<RoleVO> findById(HttpServletRequest request,RoleVO role) {
		Role r=new Role();
		RoleVO rv=new RoleVO();
		 if(SystemCons.checkLoging(request))//登录的逻辑
		    {
				try {
					 r=roleDao.findbyId(role);
				     BeanUtils.copyProperties(r, rv);
				     result.setData(rv);//返回查询数据
					 result.setErrorMsg("查询成功");
					 logger.info("角色查询--成功！");
				} catch (Exception e) {
					result.setCode(ResultCode.ERRO.getValue());
					result.setErrorMsg(ResultCode.ERRO.getName());
					logger.info("角色查询异常--信息！");
				}
			    return result;
			}else{//未登录的逻辑
				result.setCode(ResultCode.NOTLOGIN.getValue());
				result.setErrorMsg(ResultCode.NOTLOGIN.getName());
				logger.info("角色未登录");
				return result;
			}
	}



	@Override
	public ResultEntity<RoleVO> delRoleByVo(HttpServletRequest request,String roleid) {
		 if(SystemCons.checkLoging(request))//登录的逻辑
		    {
				try {
					 roleDao.deleteByPrimaryKey(roleid);
					 result.setErrorMsg("删除成功");
					 logger.info("删除--成功！");
				} catch (Exception e) {
					result.setCode(ResultCode.ERRO.getValue());
					result.setErrorMsg(ResultCode.ERRO.getName());
					logger.info("角色查询异常--信息！");
				}
			    return result;
			}else{//未登录的逻辑
				result.setCode(ResultCode.NOTLOGIN.getValue());
				result.setErrorMsg(ResultCode.NOTLOGIN.getName());
				logger.info("角色未登录");
				return result;
			}
	}


//1、
	@Override
	public String getRoleJson(RoleVO roleVo) {
		PageJson pageJson=new PageJson();
		List<Role> ulist=new ArrayList<Role>();
		try {
			   roleVo.setPage(roleVo.getPage()-1);
			   ulist=  roleDao.queryRoleListPages(roleVo);
			   pageJson.setMsg("成功");
			   pageJson.setData(ulist);
			   pageJson.setCount(roleDao.queryTotal(roleVo.getRoleName()));
		} catch (Exception exception) {
			// TODO: handle exception
			pageJson.setCode(111111);
			pageJson.setMsg(exception.getMessage());
		}
		
		 logger.info(JSON.toJSONString(pageJson));
		return  JSON.toJSONString(pageJson);
	}



	//2、获取角色对象
	@Override
	public Role getRole(RoleVO r) {
		
		return roleDao.findbyId(r);
	}



	//3、添加角色
	@Override
	public String addRoleJson(RoleVO r,HttpServletRequest request) {
		 Role rloe=new Role();
		  PageJson p=new PageJson();
		  BeanUtils.copyProperties(r, rloe);
		  rloe.setRoleId(UUID.randomUUID().toString());//生成不重复的UUID
		  //校验用户是否已登录
		  if(SystemCons.checkLoging(request)){
			  try{
				  //设置添加用户默认所属公司
				  roleDao.insertSelective(rloe);  
				  p.setCode(ResultCode.OK.getValue());
				  p.setMsg(ResultCode.OK.getName());
				  logger.info("新增用户成功");
			  }catch(Exception e){
					p.setCode(ResultCode.ERRO.getValue());
					p.setMsg(ResultCode.ERRO.getName());
					logger.info("异常信息："+e.getMessage());
			  }
		  }else{
				p.setCode(ResultCode.NOTLOGIN.getValue());
				p.setMsg(ResultCode.NOTLOGIN.getName());
				logger.info("新增用户  -- 用户未登录");
		  }
		  return JSON.toJSONString(p);
	}
	
	//4、更新角色
	public String updateRole(RoleVO r,HttpServletRequest request){
		  PageJson p=new PageJson();
		  if(SystemCons.checkLoging(request)){
			  try{
				   roleDao.updateByPrimaryKeySelective(r);
			       logger.info("修改角色信息成功");
				   p.setCode(ResultCode.OK.getValue());
				   p.setMsg(ResultCode.OK.getName());
			  }catch(Exception e){
				    p.setCode(ResultCode.ERRO.getValue());
					p.setMsg(ResultCode.ERRO.getName());
					logger.info("异常信息："+e.getMessage());
					return JSON.toJSONString(p);
			  }
		  }else{
			    p.setCode(ResultCode.NOTLOGIN.getValue());
				p.setMsg(ResultCode.NOTLOGIN.getName());
				logger.info("新增角色  -- 用户未登录");
		  }
		  return JSON.toJSONString(p);
	}
	
	//6、删除角色
	public String delRoleJson(String ids){
		PageJson pageJson=new PageJson();
		try {
			   roleDao.deleteByPrimaryKey(ids);
			   pageJson.setMsg("成功");
		} catch (Exception exception) {
			// TODO: handle exception
			pageJson.setCode(111111);
			pageJson.setMsg(exception.getMessage());
		}
		 logger.info(JSON.toJSONString(pageJson));
		return  JSON.toJSONString(pageJson);
	}
	
}
